package kxfang.com.common.core.util;

import android.view.View;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import kxfang.com.common.core.annotation.BView;
import kxfang.com.common.core.annotation.Click;

public class AnnotationUtil {

    /**
     * 初始化界面上的注解值
     */
    public static void initView(final Object target,View sourceView) {
        final Class mClass = target.getClass();
        if(mClass.getModifiers()==Modifier.PRIVATE){
            try {
                throw new IllegalAccessException("类的修饰符不能为private");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

        Field[] fields = mClass.getDeclaredFields();
        Method[] methods = mClass.getMethods();
        // 控件注解
        for (Field field : fields) {
            BView bindView = field.getAnnotation(BView.class);
            if (bindView == null) {
                continue;
            }
            if (bindView.value() == View.NO_ID) {
                throw new IllegalArgumentException("参数值不正确");
            }
//            validateMember(field);
            field.setAccessible(true);
            View view = sourceView.findViewById(bindView.value());
            try {
                Class viewClass = field.getType();
                field.set(target, viewClass.cast(view));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        // 方法注解
        for (final Method method : methods) {
            Click onClick = method.getAnnotation(Click.class);
            if (onClick == null) {
                continue;
            }
            validateMember(method);
            int[] values = onClick.value();
            for (int value : values) {
                if (value == View.NO_ID) {
                    throw new IllegalArgumentException("参数值不正确");
                }
                View view = sourceView.findViewById(value);
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            method.invoke(target, v);
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }
    }

    /**
     * 验证属性的修饰符是否为private,并设置属性可用权限
     *
     * @param o
     */
    private static <T extends AccessibleObject & Member> void validateMember(T o) {
        int modifiers = o.getModifiers();
        if ((modifiers & (Modifier.PRIVATE | Modifier.STATIC)) != 0) {
            try {
                throw new IllegalAccessException(o.getDeclaringClass().getName() + "——" + o.getName() + "属性修饰不符不能为private");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        if (modifiers == Modifier.PUBLIC || modifiers == Modifier.PROTECTED) {
            o.setAccessible(true);
        }
    }
}
